package com.tbq.game.service.utils

import android.content.pm.PackageManager
import android.util.Log
import com.tbq.game.service.app.App.Companion.context

object ApkInfoUtil {
    /**
     *
     * 获取应用程序名称
     *
     * @return 应用程序名称
     */
    fun getAppName(): String? {
        try {
            val packageManager = context?.packageManager
            val packageInfo = packageManager?.getPackageInfo(context?.packageName, 0)
            val labelRes = packageInfo?.applicationInfo?.labelRes
            return context?.resources?.getString(labelRes!!)
        } catch (e: PackageManager.NameNotFoundException) {
            Log.e("AppInfoUtil", "获取应用程序APP名称出错", e)
        }

        return null
    }
    /**
     * 获取应用程序版本名称信息
     *
     * @return 当前应用的版本名称
     */
    fun getVersionName(): String? {
        try {
            val packageManager = context?.packageManager
            val packageInfo = packageManager?.getPackageInfo(context?.packageName, 0)
            return packageInfo?.versionName
        } catch (e: PackageManager.NameNotFoundException) {
            Log.e("AppInfoUtil", "获取应用程序版本名称信息出错", e)
            e.printStackTrace()
        }

        return null
    }
    /**
     * 获取当前软件的版本号
     *
     * @return 版本号
     */
    fun getVersionCode(): Int {
        var versionCode = 0
        try {
            // 获取软件版本号，对应AndroidManifest.xml下android:versionCode
            versionCode = context?.packageManager?.getPackageInfo(context?.packageName, 0)!!.versionCode
        } catch (e: PackageManager.NameNotFoundException) {
            e.printStackTrace()
        }

        return versionCode
    }
}